class TicTacToe:
    """三连棋"""

    def __init__(self):
        self._board = [[' '] * 3 for _ in range(3)]
        self._player = 'X'

    def mark(self, i, j):
        """检查输入合法"""
        if not (0 <= i <= 2 and 0 <= j <= 2):
            raise IndexError('i,j超出棋盘')
        if self._board[i][j] != ' ':
            raise ValueError('位置不为空')
        if self.winner() is not None:
            raise ValueError('游戏已结束')
        self._board[i][j] = self._player
        if self._player == 'X':
            self._player = 'O'
        else:
            self._player = 'X'

    def _is_win(self, mark):
        board = self._board
        return (
                mark == board[0][0] == board[0][1] == board[0][2] or  # 第一行
                mark == board[1][0] == board[1][1] == board[1][2] or  # 第二行
                mark == board[2][0] == board[2][1] == board[2][2] or  # 第三行
                mark == board[0][0] == board[1][0] == board[2][0] or  # 第一列
                mark == board[0][1] == board[1][1] == board[2][1] or  # 第二列
                mark == board[0][2] == board[1][2] == board[2][2] or  # 第三列
                mark == board[0][0] == board[1][1] == board[2][2] or  # 正斜列
                mark == board[0][2] == board[1][1] == board[2][0]  # 反斜列
        )

    def winner(self):
        for mark in 'XO':
            if self._is_win(mark):
                return mark
        return None

    def __str__(self):
        rows = ['|'.join(self._board[i]) for i in range(3)]
        return '\n—————\n'.join(rows)


if __name__ == '__main__':
    game = TicTacToe()
    game.mark(1, 1); game.mark(0, 2)
    print(game)
